from rest_framework import serializers

from home.models import AxfOrder, AxfOrdergoods
from market.marketserializers import GoodsSerializer


class OrderGoodsSerializer(serializers.ModelSerializer):
    o_goods = GoodsSerializer() #订单商品跟订单是一对一关系
    class Meta:
        model = AxfOrdergoods
        fields = "__all__"


class OrderSerializer(serializers.ModelSerializer):
    # order_goods_info = OrderGoodsSerializer(many=True)
    class Meta:
        model = AxfOrder
        fields = "__all__"

    def to_representation(self, instance):
        # 调用父类方法获取序列化后的数据
        data = super().to_representation(instance)
        order_goods = instance.goods.all()
        # 序列化订单商品信息
        serializers = OrderGoodsSerializer(order_goods, many=True)
        data['order_goods_info'] = serializers.data
        return data
